package com.longxi.tianjintianbao.ui.presenter.energy_read;

import android.content.Intent;

import com.longxi.tianjintianbao.AppConst;
import com.longxi.tianjintianbao.api.response.HttpResult;
import com.longxi.tianjintianbao.listener.SelectRepairDetailListener;
import com.longxi.tianjintianbao.model.energy.EnergyRead;
import com.longxi.tianjintianbao.ui.activity.repair.RepairDetailActivity;
import com.longxi.tianjintianbao.ui.adapter.multitype.energy_read.EnergyReadListViewBinder;
import com.longxi.tianjintianbao.ui.base.BaseActivity;
import com.longxi.tianjintianbao.ui.base.BasePresenter;
import com.longxi.tianjintianbao.ui.view.energy_read.IEnergyReadListFgView;
import com.longxi.tianjintianbao.utils.NListRefresh;

import java.util.ArrayList;
import java.util.List;

import me.drakeet.multitype.Items;
import me.drakeet.multitype.MultiTypeAdapter;

public class EnergyReadListFgPresenter extends BasePresenter<IEnergyReadListFgView> {

    private MultiTypeAdapter adapter;
    private Items items;

    public EnergyReadListFgPresenter(BaseActivity context) {
        super(context);
    }

    public void initAdapter(){
        if(adapter == null){
            adapter = new MultiTypeAdapter();
            SelectRepairDetailListener listener = new SelectRepairDetailListener() {
                @Override
                public void onSuccess(String buildId) {
                    Intent mainIntent = new Intent(mContext, RepairDetailActivity.class);
                    mainIntent.putExtra(AppConst.BILLID,buildId);
                    mContext.startActivityForResult(mainIntent, AppConst.IntentRequstCode.ACTIVITY_UPDATE_REPAIR_CONTENT);
                }
            };
            /* 注册类型和 View 的对应关系 */
            adapter.register(EnergyRead.class, new EnergyReadListViewBinder(listener));
            getView().getRvContent().setAdapter(adapter);
        }
    }

    public void getRepairList(int page, String state){
//        JSONObject object = new JSONObject();
//        try {
//            object.put("userid", MyApplication.loginUser.userid);
//            object.put("buildid", buildId);
//            object.put("state", state);
//            object.put("page", page);
//            object.put("rowcount", 10);
//        } catch (JSONException e) {
//            e.printStackTrace();
//        }
//
//        ApiRetrofit.getInstance().getEventRepairList(object)
//        .subscribeOn(Schedulers.io())
//        .observeOn(AndroidSchedulers.mainThread())
//        .subscribe(new MySubscriber<RepairListResponse>(mContext) {
//            @Override
//            public void onError(ExceptionHandle.ResponeThrowable throwable) {
//                mContext.hideWaitingDialog();
//                MyUtils.showToast(throwable.message);
//            }
//
//            @Override
//            public void onNext(RepairListResponse repairListResponse) {
//                fjData(repairListResponse);
//            }
//        });
        getData();
    }

    /**
     * 测试数据
     */
    public void getData(){
        List<EnergyRead> items2 = new ArrayList<>();
        EnergyRead repairList = new EnergyRead();
        items2.add(repairList);
        items2.add(repairList);
        items2.add(repairList);
        items2.add(repairList);
        items2.add(repairList);

        HttpResult<EnergyRead> result = new HttpResult<>();
        result.setCode(0);
        result.setData(items2);

        items = NListRefresh.getInstance().fjData(result,
                getView().getRefreshLayout(),
                items,
                adapter,
                mFragment);
    }

}
